//
//  XFBrandView.m
//  WorkManager
//
//  Created by ukongm on 17/1/19.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFBrandView.h"

@interface XFBrandView()

/** 图片 */
@property(nonatomic,strong)UIImageView* imageBtn;
/** 位子 */
@property(nonatomic,strong)UILabel* valueLb;

@end

@implementation XFBrandView
{
    XFBrandViewStyle _style; //保存用户设置的风格
    BOOL _network; //图片是否网路加载
}
- (UILabel *)valueLb{
    
    if (_valueLb == nil) {
        _valueLb = [[UILabel alloc] initWithFrame:CGRectZero];
        _valueLb.font = [UIFont systemFontOfSize:15];
        _valueLb.textColor = [UIColor red:70 green:70 blue:70];
        _valueLb.textAlignment = NSTextAlignmentCenter;
    }
    return _valueLb;
}

- (UIImageView *)imageBtn{
    
    if (_imageBtn == nil) {
        _imageBtn = [[UIImageView alloc] initWithFrame:CGRectZero];
        _imageBtn.userInteractionEnabled = NO;
//        _imageBtn.backgroundColor = [UIColor arcColor];
    }
    return _imageBtn;
}

- (instancetype)initWithFrame:(CGRect)frame andStyle:(XFBrandViewStyle)style{
    return [self initWithFrame:frame andStyle:style andNetwork:NO];
}

- (instancetype)initWithFrame:(CGRect)frame andStyle:(XFBrandViewStyle)style andNetwork:(BOOL)network{
    if (self = [super initWithFrame:frame]) {
        _style = style;
        _network = network;
        
        [self createView];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    
    [self createView];
}

//添加视图
- (void)createView{
    switch (_style) {
        case XFBrandViewStyleTopFont:
            //文字在上面
            self.valueLb.frame = CGRectMake(0, 0, self.width, BARND_LABEL_HEIGHT);
            self.imageBtn.frame = CGRectMake(0, self.valueLb.maxY, self.width, self.height - self.valueLb.height);
            break;
        case XFBrandViewStyleBottomFont:
            //文字在下面
            self.imageBtn.frame = CGRectMake(0, 0, self.width, self.height - self.valueLb.height);
            self.valueLb.frame = CGRectMake(0, self.imageBtn.maxY, self.width, BARND_LABEL_HEIGHT);
            break;
            
        default:
            break;
    }
    
    [self addSubview:self.valueLb];
    [self addSubview:self.imageBtn];
}

- (void)setImageName:(NSString *)imageName{
    if (_network) {
        
        [self.imageBtn sd_setImageWithURL:[NSURL URLWithString:imageName] placeholderImage:[UIImage imageNamed:self.placeImageName] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            if (image == nil) {
                return ;
            }
            
            if (!self.isHead) {
                CGSize size = [image mathSizeWithMaxWidth:self.imageBtn.width * 0.8 anMaxHeight:self.imageBtn.height * 0.66];
                
                [self.imageBtn changeSize:size];
                
                self.isRad = self.isRad;
            }
            
        }];
    }else{
        [self.imageBtn setImage:[UIImage imageNamed:imageName]];
        
    }
}

- (void)setValue:(NSString *)value{
    
    self.valueLb.text = value;
    
}

- (void)setValueFont:(UIFont *)valueFont{
    self.valueLb.font = valueFont;
}

- (void)setIsRad:(BOOL)isRad{
    _isRad = isRad;
    
    self.imageBtn.layer.cornerRadius = self.imageBtn.height/2;
    self.imageBtn.layer.masksToBounds = YES;
}

- (void)setValueColor:(UIColor *)valueColor{
    self.valueLb.textColor = valueColor;
}


@end
